function onGCKeyPress(e)
{
    switch (e.keyCode)
    {
	case 13: /* CR */
	    e.line = e.target.value;
	    e.target.value = "";
	    if (e.line.search(/\S/) <0)
		return;
	    groupChat(e.line);
	    break;
    }
}

RenderNick.prototype.removeElement = function removeNickname(ds, source, property, target)
{
	var thread = source.Value.substr(0, source.Value.lastIndexOf("/"));
	if(thread!= "urn:xmpp:muc:member:" + window.name || property.Value != "urn:xmpp:muc:member#status")
		return;
	var fullid = source.Value.substr(source.Value.lastIndexOf(":")+1);
	var doc = this.box.ownerDocument;
	var nickname = doc.getElementById(fullid);
	if (nickname)
		doc.removeChild(nickname);
}

RenderNick.prototype.addElement = function addNickname(ds, source, property, target)
{
	var thread = source.Value.substr(0, source.Value.lastIndexOf("/"));
	if(thread!= "urn:xmpp:muc:member:" + window.name || property.Value != "urn:xmpp:muc:member#status")
		return;
	renderNick.paint(source);
}


Jazzila.prototype.onClick = function (event)
{
    switch (event.button)
    {
	case 2:
	    this.showContextMenu(event.clientX, event.clientY, "groupchat-context-menu", "output");		
	    break;
    };
}

function groupChat(msg)
{
    var thread = window.name;
    jazzila.nsIXmppService.groupchat(thread, msg);
}

function customMucLeave ()
{

}

function mucLeave()
{
	this.nsIXmppService.leave(window.name);
}

function getNicknamesTableRow(nick){
	var doc = renderNick.box.ownerDocument;
	var cell = doc.createElement("html:td");
	cell.appendChild(nick);
	var row = doc.createElement("html:tr")
	row.appendChild(cell);
	return(row);
}

function getHtmlNick(elem)
{
	var jid = elem.Value;
	var name = getRDFLiteral(ds, elem, this.nick);
	jid = jid.substr(jid.lastIndexOf(":")+1);
	var row = getNicknamesTableRow(getDiv(name));
	row.setAttribute("id", jid);
	this.box.appendChild(row);
}

function RenderNick (box)
{
    this.rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
    this.cu = CC["@mozilla.org/rdf/container-utils;1"].getService(CI.nsIRDFContainerUtils); 
    this.nick = this.rdf.GetResource("urn:xmpp:muc:member#nick");
    this.room = this.rdf.GetResource("urn:xmpp:muc:room:"+window.name);
    this.box = box;
}

function getXulNick(elem)
{
    var nick = this.doc.createElement("nickname");
    return nick;
}

RenderNick.prototype.render = function (box)
{
    var ctn = this.cu.MakeBag(ds, this.room);
    var etor = ctn.GetElements();
    while (etor.hasMoreElements())
    {
	var elem = etor.getNext().QueryInterface(CI.nsIRDFResource);
	this.paint(elem);
    }
}

RenderNick.prototype.paint = function ()
{
}

function onGroupChatLoad()
{
  var frame = document.getElementById("nickframe");
  var room= window.name;
  if (frame) 
  {
	window.removeEventListener("load", onGroupChatLoad, true);
	var box = frame.contentDocument.getElementById("nicknames");
	renderNick = new RenderNick(box);
	renderNick.paint = getHtmlNick;
	if (box)
	{
	     renderNick.render(box);
	     jnl = new TreeListener();
	     jnl.onAssert = renderNick.addElement;
	     jnl.onUnassert = renderNick.removeElement;
	     ds.AddObserver(jnl);
	     jazzila.sendMessage = groupChat;
  	     jazzila.nsIXmppService.join(room, nick);
    }
	setFocus = setGCFocus;
	setFocus();
   }
}

function setGCFocus()
{
	var say = document.getElementById("groupchat-say");
	if (say)
	{
		say.focus();
	}
}

function onGroupChatUnload()
{
  window.removeEventListener("close", onGroupChatUnload, true);
  if (jnl)
	ds.RemoveObserver(jnl);
  jazzila.nsIXmppService.leave(window.name, null);
  delete jnl;
}

var jnl = null;
var renderNick = null;
var nick = null;
if (window.arguments.length)
{
    nick = window.arguments[0];
}

window.addEventListener("load", onGroupChatLoad, true);
window.addEventListener("close", onGroupChatUnload, true);